var app = getApp();
const apiHost = require('../../common/api_host.js');
var utils = require('../../utils/utils.js');
var httpJson=require('../../utils/http_json.js');
Page({
  inputName:function(e){
    this.setData({
      name:e.detail.value
    })

  },
  ok:function(){
    this.setData({
      haveReg:1
    })
    // wx.navigateTo({
    //   url:"../authorize/authorize?myid="+100
    // })
   
    // app.verifyLogin(()=>{ })
   
         
    
   
  },
  getAge: function(e) {
    console.log("年龄",e)
   
  
    this.setData({
      index: parseInt( e.detail.value),
     
    })
    
  },
  getSex:function(e){
    this.setData({
      sexIndex: e.detail.value,
     
    })
  },
  goin:function(){
  
       /**1.1向服务器发送get请求 */
       let url = `${apiHost.config.portalApiHost}/portal/user/update`;
       var data={
        userName:this.data.nickName,
        headerImg:this.data.avatarUrl,
        gender:this.data.sexIndex==0?'WOMAN':"MAN",
        age:this.data.ages[this.data.index],
        city:this.data.city


       }
       httpJson.post(url,data,res=>{
         if(res.data.errorCode==0){
           var _this=this;
          wx.showModal({
            title: '确认提交？',
            content:this.data.sexs[this.data.sexIndex]+ '确认提交基本信息吗?',
            success(res) {
              if(res.confirm){
                if(_this.data.index>13){
                  _this.setData({
                    overAge:1
                  })
                  setTimeout(()=>{
                    _this.setData({
                      overAge:0
                    })
                    if(_this.data.haveReg==1){
                       wx.reLaunch({
                        url: '../index/index/index?msg=reg'
                      })
                    }
                    else if(_this.data.haveReg==3){
                      wx.navigateBack()
                   }
                   
                  },2000)
                  
                }else{
                  wx.reLaunch({
                    url: '../index/index/index?msg=reg'
                  })
                }

              }

            }
          })
         }
       })
    
  


  },
  jump:function(){
     /**1.1向服务器发送get请求 */
      let url = `${apiHost.config.portalApiHost}/portal/user/update`;
      var data={
        userName:this.data.nickName,
        headerImg:this.data.avatarUrl,
        gender:this.data.sexIndex==0?'WOMAN':"MAN",
        age:this.data.ages[this.data.index],
        city:this.data.city
      }
      httpJson.post(url,data,res=>{
        if(res.data.errorCode==0){
          wx.reLaunch({
            url: '../index/index/index?msg=reg'
          })
        
        }
      })
    
  },
  inputname:function(e){
    this.setData({
      changeName:true,
      nickName:e.detail.value
    })
    // console.log(e)

  },
  getuser:function(){
    var  url=`${apiHost.config.portalApiHost}/portal/user/update`;
   
    httpJson.post(url,{},res=>{
      if(res.data.errorCode==0){
        let userMsg=res.data.body;
//设置当前存储的年龄和性别
        this.data.ages.forEach((ele,idx) => {
            if(ele==userMsg.age){
              this.setData({
                index:idx
              })
            }
          
        });
        if(userMsg.gender=='WOMAN'){
          this.setData({
            sexIndex:0
          })
        }
        else if(userMsg.gender=='MAN'){
          this.setData({
            sexIndex:1
          })
        }
        this.setData({
          userMsg:res.data.body
        })

      }
    })
  },
  // changeAva:function(){
  //   let _this=this
  //   wx.chooseImage({
  //     count: 1,
  //     sizeType: ['compressed'], // 'original', 
  //     sourceType: ['album', 'camera'],
  //     success: function (res) {
  //       // console.log(res)
  //       _this.setData({
  //         avatarUrl:res.tempFilePaths[0]
  //       })
  //     }
  //   });

  // },

  changeAva:function(){
    var _this=this;
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: `${apiHost.config.portalApiHost}/v1/base/upload/ueditor_uploadimage`, // 仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'upfile',
          
          success(res) {
            let data = res.data;
            data=JSON.parse(data)
           
            _this.setData({
              changeAva:true,
              avatarUrl:data.url
            })
            // do something
          }
        })
      }
    })
  },

  /**
   * 页面的初始数据
   */
  data: {
    //正常是0
    haveReg:0,
    ages:["9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26"],
    sexs:['小仙女',"小鲜肉"],
    sexIndex:0,
    index:9,
    overAge:0,
    changeAva:false,
    changeName:false
    


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.verifyLogin(()=>{
      let _this=this
      wx.getUserInfo({
        lang:'zh_CN',
        success(res) {
          const userInfo = res.userInfo
          const nickName = userInfo.nickName
          let avatarUrl=userInfo.avatarUrl
          const city = userInfo.city
          _this.setData({
            nickName,avatarUrl,city
          })
        }
        
      })

    })
    //来自我的页面，此时想修改信息
    if(options.myid==3){
      this.getuser()
      this.setData({
        haveReg:3
      })
     
     
    }

  },
  change:function(){
    wx.navigateBack()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(res) {
    app.share()
    if (res.from === 'button') {
      var content=res.target.dataset.content
     
      if(content.length>50){
        content=content.substr(0,50)+'...'
      }
      var title=content
    }
    if(res.from === 'menu'){
      var imageUrl="/images/index/reply/reply.png";
      var title='我正在惊人院，邀请你来……'
    }
    return {
      title,
      imageUrl
      
    }
  }
})